package com.example.anpnovel.controller.crud;

import com.example.anpapi.entity.po.novelmodel.Volume.Volume;
import com.example.anpapi.entity.vo.R;
import com.example.anpnovel.service.interfaces.VolumeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/volume")
public class VolumeController {

    @Autowired
    private VolumeService volumeService;

    /**
     * 卷添加
     * @param volume
     * @return
     */
    @PostMapping("/save")
    public R<Volume> save(@RequestBody Volume volume){
        log.info("卷添加:{}", volume);
        volumeService.save(volume);
        return R.success(volume,"卷添加成功");
    }

    /**
     * 根据卷id删除卷
     * @param volume
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody Volume volume){
        log.info("卷删除:{}", volume);
        if(volumeService.getById(volume.getId()) != null)
        {
            volumeService.removeById(volume.getId());
            return R.success(null,"卷删除成功");
        }
        return R.error("未找到该卷");
    }

    /**
     * 根据卷id查找卷
     * @param id
     * @return
     */
    @GetMapping("/get")
    public R<Volume> get(@RequestParam(value = "id") Long id) {
        log.info("查询卷的id为{}", id);
        Volume volume = volumeService.getById(id);
        return R.success(volume, "查询卷成功");
    }

    /**
     * 根据作品id查找所有卷
     * @param novelId
     * @return
     */
    @GetMapping("/getByNovelId")
    public R<List<Volume>> getByNovelId(@RequestParam(value = "novelId") Long novelId) {
        log.info("查询作品id为{}的所有卷", novelId);
        List<Volume> volumes = volumeService.lambdaQuery()
                .eq(Volume::getNovelId, novelId)
                .orderByAsc(Volume::getSortOrder)
                .list();
        return R.success(volumes, "查询卷成功");
    }

    /**
     * 修改卷
     * @param volume
     * @return
     */
    @PostMapping("/update")
    public R<Volume> update(@RequestBody Volume volume){
        log.info("修改的卷为{}", volume);
        volumeService.updateById(volume);
        return R.success(volume,"修改卷成功");
    }

}

